// +----------------------------------------------------------------------
// | JavaWeb_EleVue_Pro前后端分离旗舰版框架 [ JavaWeb ]
// +----------------------------------------------------------------------
// | 版权所有 2021 上海JavaWeb研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.javaweb.vip/
// +----------------------------------------------------------------------
// | 作者: 鲲鹏 <javaweb520@gmail.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

package com.javaweb.admin.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.admin.entity.C37InfCkScBillNew;
import com.javaweb.admin.entity.OrgCodeRefer;
import com.javaweb.admin.service.IOrgCodeReferService;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.utils.CommonUtils;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.admin.constant.AnnualBudgetConstant;
import com.javaweb.admin.entity.AnnualBudget;
import com.javaweb.admin.mapper.AnnualBudgetMapper;
import com.javaweb.admin.query.AnnualBudgetQuery;
import com.javaweb.admin.service.IAnnualBudgetService;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.system.utils.ShiroUtils;
import com.javaweb.admin.vo.annualbudget.AnnualBudgetInfoVo;
import com.javaweb.admin.vo.annualbudget.AnnualBudgetListVo;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.OverridesAttribute;
import java.io.Serializable;
import java.util.*;

/**
  * <p>
  * 年度预算表 服务类实现
  * </p>
  *
  * @author 鲲鹏
  * @since 2023-04-23
  */
@Service
public class AnnualBudgetServiceImpl extends BaseServiceImpl<AnnualBudgetMapper, AnnualBudget> implements IAnnualBudgetService {

    @Autowired
    private AnnualBudgetMapper annualBudgetMapper;

    @Autowired
    private IOrgCodeReferService orgCodeReferService;

    /**
     * 获取数据列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    @SpecifyDataSource(value = DataSourceType.BIP)
    public JsonResult getList(BaseQuery query) {
        AnnualBudgetQuery annualBudgetQuery = (AnnualBudgetQuery) query;
        // 查询条件
        QueryWrapper<AnnualBudget> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id,yearly,purchase_code,sale_code,material_code,internal_price,purchase_num,purchase_amount,purchase_margin,sales_unit_price,sale_num,sale_amount,sale_margin,variable_cost,data_source,classification,status");
        queryWrapper.orderByDesc("id");
        if(!StringUtils.isEmpty(annualBudgetQuery.getStatus()))
        {
            queryWrapper.eq("status",annualBudgetQuery.getStatus());
        }
        if(!StringUtils.isEmpty(annualBudgetQuery.getYearly()))
        {
            queryWrapper.eq("yearly",annualBudgetQuery.getYearly());
        }
        if(!StringUtils.isEmpty(annualBudgetQuery.getMaterialCode()))
        {
            queryWrapper.like("material_code",annualBudgetQuery.getMaterialCode());
        }
        if(!StringUtils.isEmpty(annualBudgetQuery.getPurchaseCode()))
        {
            queryWrapper.like("purchase_code",annualBudgetQuery.getPurchaseCode());
        }
        if(!StringUtils.isEmpty(annualBudgetQuery.getSaleCode()))
        {
            queryWrapper.like("sale_code",annualBudgetQuery.getSaleCode());
        }

        // 获取数据列表
        IPage<AnnualBudget> page = new Page<>(annualBudgetQuery.getPage(), annualBudgetQuery.getLimit());
        IPage<AnnualBudget> pageData = annualBudgetMapper.selectPage(page, queryWrapper);
        pageData.convert(x -> {
            AnnualBudgetListVo annualBudgetListVo = Convert.convert(AnnualBudgetListVo.class, x);
            return annualBudgetListVo;
        });
        return JsonResult.success(pageData);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.BIP)
    public List<AnnualBudget> GetWSCList() {
        QueryWrapper<AnnualBudget> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        // queryWrapper.orderByDesc("id");
        queryWrapper.eq("status","Y").eq("mark",1);

        return super.list(queryWrapper);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.BIP)
    public AnnualBudget GetDetail(Integer id) {
        QueryWrapper<AnnualBudget> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id,yearly,purchase_code,sale_code,material_code,internal_price,purchase_num,purchase_amount,purchase_margin,sales_unit_price,sale_num,sale_amount,sale_margin,variable_cost");
        // queryWrapper.orderByDesc("id");
        queryWrapper.eq("id",id);

        return super.list(queryWrapper).get(0);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.BIP)
    public String GetBIPOrgCode(String saleCode) {
        QueryWrapper<OrgCodeRefer> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("bip_org_code");
        queryWrapper.eq("fine_org_code",saleCode).eq("enable",1);
        OrgCodeRefer codeRefer = orgCodeReferService.getOne(queryWrapper);
        return codeRefer!=null?codeRefer.getBipOrgCode():"";

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.BIP)
    public String GetFineOrgCode(String bipCode) {
        QueryWrapper<OrgCodeRefer> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("fine_org_code");
        queryWrapper.eq("bip_org_code",bipCode).eq("fine_flag",1);
        OrgCodeRefer codeRefer = orgCodeReferService.getOne(queryWrapper);
        return codeRefer!=null?codeRefer.getFineOrgCode():"";

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.BIP)
    public JsonResult getYearList() {
        List<String> list = annualBudgetMapper.getYearList();
        return  JsonResult.success(list);
    }


    @Override
    @SpecifyDataSource(value = DataSourceType.BIP)
    public boolean UpdateZT(Integer id, String zt) {
        try {
            AnnualBudget entity = new AnnualBudget();
            //entity.setDanjNo(danj_no);
            UpdateWrapper<AnnualBudget> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("status", zt).set("sync_time",DateUtils.getTime()).eq("id", id).eq("status","Y");
            return this.update(entity, updateWrapper);
        }
        catch(Exception ex)
        {
            return false;
        }

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.BIP)
    public boolean UpdateMark(Integer id) {
        try {
            AnnualBudget entity = new AnnualBudget();
            //entity.setDanjNo(danj_no);
            UpdateWrapper<AnnualBudget> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("mark", 0).set("sync_time",DateUtils.getTime()).eq("id", id);
            return this.update(entity, updateWrapper);
        }
        catch(Exception ex)
        {
            return false;
        }

    }
    /**
     * 获取详情Vo
     *
     * @param id 记录ID
     * @return
     */
    @Override
    public Object getInfo(Serializable id) {
        AnnualBudget entity = (AnnualBudget) super.getInfo(id);
        // 返回视图Vo
        AnnualBudgetInfoVo annualBudgetInfoVo = new AnnualBudgetInfoVo();
        // 拷贝属性
        BeanUtils.copyProperties(entity, annualBudgetInfoVo);
        return annualBudgetInfoVo;
    }

    /**
     * 添加、更新记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult edit(AnnualBudget entity) {
        if (StringUtils.isNotNull(entity.getId()) && entity.getId() > 0) {
        } else {
            entity.setCreateTime(DateUtils.now());
        }
        return super.edit(entity);
    }

    /**
     * 删除记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult delete(AnnualBudget entity) {
        entity.setMark(0);
        return super.delete(entity);
    }

}